var sqlserver = require('mssql');
const dotenv = require('dotenv');
dotenv.config();

var dbConfig = {
    server: process.env.MSSQLDB_HOST,
    port: process.env.MSSQLDB_PORT,
    user: process.env.MSSQLDB_USER,
    password: process.env.MSSQLDB_PASSWORD,
    database: process.env.MSSQLDB_NAME,
    options: {
        encrypt: false,
        useUTC: false
    }
};

var mssql_async= function (strsql) {
    return new Promise(function (resolve, reject) {
        sqlserver
            .connect(dbConfig)
            .then(function () {
                console.log('conn success');  //使用连接池，因此不需要显式关闭连接
                new sqlserver.Request()
                    .query(strsql)
                    .then(function (recordset) {
                        resolve(recordset);
                    })
                    .catch(function (err) {
                        reject(err);
                    });
            })
            .catch(function (err) {
                console.log('conn error', err);
                reject(err);
            });
    });
};
module.exports = mssql_async;
